package org.monster.context.core.processor;

import org.monster.app.AppContext;
import org.monster.context.core.parameter.HttpResponse;
import org.monster.context.core.request.HttpRequest;

/**
 * 处理应用
 *
 * @author Monster
 */
public class ProcessApps {

    /**
     * 应用上下文
     */
    private AppContext context;

    /**
     * 构造方法
     *
     * @param context 应用上下文
     */
    public ProcessApps(AppContext context) {
        this.context = context;
    }

    /**
     * 处理
     *
     * @param request  请求
     * @param response 响应
     * @throws ClassNotFoundException 类未找到异常
     */
    public void process(HttpRequest request, HttpResponse response) throws ClassNotFoundException {
        String headerInfo = request.getHeaderInfo();
        if (headerInfo != null) {
            String method = headerInfo.substring(3, headerInfo.length()).trim();
            int index = method.indexOf(' ');
            if (index != -1) {
                String dosCommand = method.substring(0, index);
                String dosParameter = method.substring(index + 1,
                        method.length());
                context.execute(dosCommand, dosParameter);
            }
        }
    }
}
